package com.zzmltc.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zzmltc.hosp.repository.DepartmentRepository;
import com.zzmltc.hosp.service.DepartmentService;
import com.zzmstc.model.hosp.Department;
import com.zzmstc.vo.hosp.DepartmentQueryVo;
import com.zzmstc.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;


//  上传科室接口
    @Override
    public void save(Map<String, Object> paratMap) {
//        paratMap转换成department
        String paratMaptoString = JSONObject.toJSONString(paratMap);
        Department department= JSONObject.parseObject(paratMaptoString,Department.class);
//        根据医院编号和科室编号进行查询
        Department departmentByHoscodeAndDepcode = departmentRepository
                .getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepname());
//        不存在进行添加
//        存在进行修改
        if (departmentByHoscodeAndDepcode !=null){
            departmentByHoscodeAndDepcode.setUpdateTime(new Date());
            departmentByHoscodeAndDepcode.setIsDeleted(0);
            departmentRepository.save(departmentByHoscodeAndDepcode);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
         departmentRepository.save(department);
        }

    }
// 查询科室接口
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        // 创建Pageable对象，设置当前页和每页记录数
        //0是第一页
        Pageable pageable = PageRequest.of(page-1,limit);
        // 创建Example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department,matcher);

        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }
//  删除科室接口
    @Override
    public void remore(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }
    // 根据医院编号查询科室信息
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
//        创建一个list集合，用去最终数据封装
        List<DepartmentVo> result=new ArrayList<>();

//        通过查询mangodb根据根据医院编号，查询所有科室信息
        Department department = new Department();
        department.setHoscode(hoscode);
        Example example = Example.of(department);

//        所有的科室列表信息=》departmentList
        List<Department> departmentList = departmentRepository.findAll(example);

//        根据大科室的编号做一个分组 bigcode 获取每个大科室里面的子科室
        Map<String, List<Department>> deparmenMap = departmentList
                .stream()
                .collect(Collectors.groupingBy(Department::getBigcode));

//        遍历Map集合
        for(Map.Entry<String,List<Department>> entry:deparmenMap.entrySet()){
//            大科室编号
            String bigCode = entry.getKey();

//            大科室编号对应的全局数据
            List<Department> department1List=entry.getValue();

//            封装大科室
            DepartmentVo departmentVo1=new DepartmentVo();
            departmentVo1.setDepcode(bigCode);
            departmentVo1.setDepname(department1List.get(0).getBigname());

//            封装小科室
            List<DepartmentVo> children = new ArrayList<>();

            for (Department department1 :department1List){
                DepartmentVo departmentVo2=new DepartmentVo();
                departmentVo2.setDepcode(department1.getDepcode());
                departmentVo2.setDepname(department1.getDepname());
//                封装到list集合
                children.add(departmentVo2);
            }

//            把小科室的list集合放到大科室的children里面
            departmentVo1.setChildren(children);
//              放到最终的result里面
            result.add(departmentVo1);

        }

        return result;
    }
    //  根据科室编号和医院编号查询科室名称
    @Override
    public String getDepName(String hoscode,String depcode) {
        Department departmentByHoscodeAndDepcode = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (departmentByHoscodeAndDepcode!=null){
            return departmentByHoscodeAndDepcode.getDepname();
        }
        return null;

    }
    // 根据科室编号和医院编号查询科室信息
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department departmentByHoscodeAndDepcode = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return departmentByHoscodeAndDepcode;
    }


}
